<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="700px" @ok="handleSubmit" destroyOnClose>
    <MyTableAlert 
      type="warning" style="margin-bottom: 10px; " 
      message="新增接收人后，系统将自动 发送验证信息 到所填手机号和邮箱，通过验证后 方可接收消息。"
    />
    <BasicForm @register="registerForm" ref="formRef">
      <template #wxMpOpenId="{ model, field }">
        <a-row :gutter="8">
          <a-col :sm="24" :md="24" :lg="24" :xl="24">
            <a-input v-model:value="model[field]" class="input-verify-code" placeholder="扫码下方二维码自动填充" readonly/>
          </a-col>
          <a-col :sm="24" :md="24" :lg="24" :xl="24" style="margin-top: 5px;">
            <ImagePreview :image-list="[{ src: model['wxMpQrCodeUrl'], width: 80, height: 80 }]" />
          </a-col>
        </a-row>
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import {computed, onMounted, ref, unref, useAttrs} from 'vue';
import {BasicForm, useForm} from '/@/components/Form';
import {ImagePreview} from '/@/components/Preview/index';
import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
import {formSchema} from '../receiver.data';
import {getReceiver, getWxMpQrcode, saveOrUpdateReceiver} from '../receiver.api';
import MyTableAlert from '/@/components/My/MyTableAlert.vue';
import pubsub from 'pubsub-js'

// 声明Emits
  const emit = defineEmits(['success', 'register']);
  const attrs = useAttrs()
  const isUpdate = ref(true);
  const formRef = ref()
  const [registerForm, {setProps, resetFields, setFieldsValue, validate}] = useForm({
    schemas: formSchema,
    labelWidth: 100,
    showActionButtonGroup: false,
    labelCol: {
      xs: { span: 4 },
      sm: { span: 4 },
    },
    wrapperCol: {
      xs: { span: 24 },
      sm: { span: 20 },
    },
  });
  const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {
    resetFields();
    isUpdate.value = !!data?.isUpdate;
    setDrawerProps({confirmLoading: false, loading: true})
    try {

      if (unref(isUpdate)) {
        const record = await getReceiver({ id: data.record.id})
        setFieldsValue({ ...record });
      } else {
        const wxMpQrCodeUrl = await getWxMpQrcode()
        setFieldsValue({ wxMpQrCodeUrl });
      }
    } finally {
      setDrawerProps({loading: false})
    }
  });

  const getTitle = computed(() => (!unref(isUpdate) ? '新增接收人' : '编辑接收人'));
  onMounted(() => {
    pubsub.subscribe('pushData', function(msgName, params) {
      if (params.data.bizType === 'getOpenId') {
        console.log('订阅消息成功, msgName: ', msgName, ', params: ', params);
        formRef.value && setFieldsValue({ wxMpOpenId: params.data.content });
      }
    })
  })
  async function handleSubmit() {
    try {
      const values = await validate();
      setDrawerProps({confirmLoading: true});
      //提交表单
      await saveOrUpdateReceiver(values, isUpdate.value);
      closeDrawer();
      emit('success');
    } finally {
      setDrawerProps({confirmLoading: false});
    }
  }
</script>
